<?php

namespace XiaoZhuBundle\Controller\Api;

use AdminsBundle\Entity\Activity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use XiaoZhuBundle\Helper\MyController;
/**
 * @Route("/Api")
 */
class ActivityController extends MyController
{
    /**
     * @Route("/Activity", name="ApiActivity")
     */
    public function ApiActivity(Request $request)
    {
        //获取分页信息
        if (!$type = $this->getParamRequired("type",$request)) {return $this->json_out(0,"参数缺失","type");}
        if (!$p = $this->getParamRequired("pagenum",$request)) {$p = 1;}
        if (!$perPage = $this->getParamRequired("pagesize ",$request)) {$perPage = 10;}
        $param["page"] = $p;
        $param["per_page"] = $perPage;
        $rep = $this->getDoctrine()->getRepository("AdminsBundle:Activity");
        $activities = $rep->findBy(array("type"=>$type,),array("orders"=>"ASC"),$perPage,($p-1)*$perPage);
        $re = array();
        foreach ($activities as $act) {
            $re[] = array(
                "id"                => $act->getId(),
                "title"             => $act->getTitle(),
                "img"               => $act->getCover(),
                "priseNum"          => 99,
                "priseStatus"       => 0,
            );
        }
        return $this->json_out(1,"",$re);
    }


    /**
     * @Route("/Activity/Products", name="ApiActivityProducts")
     */
    public function ApiActivityProducts(Request $request)
    {
        if (!$id = $this->getParamRequired("id",$request)) {
            return $this->json_out(0,"缺少参数","ID");
        }
        $rep = $this->getDoctrine()->getRepository("AdminsBundle:Activity");
        $activity = $rep->find(array("id"=>$id,));
        $conditions = json_decode($activity->getConditions(),true);
        //筛选参数创建
        $param = array();
        $param["andWhere"]["attr"] = $conditions;
        //取出数据
        $Model = $this->get("xiao_zhu.product_model");
        $data = $Model->listProduct($param);

        $re = array();
        foreach ($data as $d) {
            $re[] = array(
                "id"            => $d["id"],
                "img"           => $d["cover"],
                "title"         => $d["name"],
                "classes"       => $d["cid"],
                "price"         => $d["price"],
                "oprice"        => $d["marketPrice"],
                "praise"        => $d["collect"],
                "isNew"         => $d["isNew"],
                "isHot"         => $d["isHot"],
                "isSale"        => ceil($d["price"]/$d["marketPrice"]*10),
                "isFamous"      => $d["isFamous"],
            );
        }
        return $this->json_out(1,"",$re);
    }


    /**
     * @Route("/Activity/Category", name="ApiCategoryPics")
     */
    public function ApiCategoryPics(){
        $return = array(
            array(
                "classId"       => 2,
                "className"     => "项饰",
                "img"           => "/app/cat/01.jpg",
            ),
            array(
                "classId"       => 3,
                "className"     => "耳饰",
                "img"           => "/app/cat/02.jpg",
            ),
            array(
                "classId"       => 4,
                "className"     => "发饰",
                "img"           => "/app/cat/03.jpg",
            ),
            array(
                "classId"       => 1,
                "className"     => "全部饰品",
                "img"           => "/app/cat/04.png",
            ),
        );
        return $this->json_out(1,"",$return);
    }
}
